package com.qf.zyh.web.controller;

import com.qf.zyh.common.Const;
import com.qf.zyh.entity.Address;
import com.qf.zyh.entity.User;
import com.qf.zyh.services.AddressSer;
import com.qf.zyh.services.Impl.AddressSerImpl;
import com.qf.zyh.util.StrUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/addr")
public class AddressCont extends BaseServlet{
    AddressSer as = new AddressSerImpl();
    //查询所有地址
    public String getAddr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户地址属性
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        if (user==null) {
            return Const.SysConst.REDIRECT+"iogin.jsp";
        }
        //根据uid查找用户地址数据
        List<Address> list =as.seleAddr(user.getUid());
        request.setAttribute("list",list);
        return Const.SysConst.FORWARD+"self_info.jsp";
    }
    //新增
    public String addAddr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        //批量获取数据，注意前端页面name值与数据库字段保持一致
        Map<String, String[]> parameterMap = request.getParameterMap();
        Address ar = new Address();
        BeanUtils.populate(ar,parameterMap);
        System.out.println(ar);
        ar.setUid(user.getUid());
        //System.out.println(ar);
   //     String name = request.getParameter("name");
   //     String phone = request.getParameter("phone");
    //    String detail = request.getParameter("detail");
        // 获取输入的名字电话地址，uid由登录的账号获取，默认为0就是普通地址
     //   Address ar = new Address(user.getUid(),name,phone,detail,0);
         boolean a =as.addAddr(ar);
         return Const.SysConst.REDIRECT+"addr?method=getAddr";
    }
    //删除地址
    public String deleAddr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取地址给与的aid直接删除即可
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        boolean aa = as.deleAddr(aid);
        return Const.SysConst.REDIRECT+"addr?method=getAddr";
    }
    //设置默认：方法为，根据aid设置为1默认地址.将其他的设置为0普通地址
    public String updaAddr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //设置单个为默认可直接按aid，但是设置其他为普通必须是该用户的地址为普通，需要传入用户uid
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        boolean a = as.updastate(aid,user.getUid());
        return Const.SysConst.REDIRECT+"addr?metnod=getAddr";
    }
    //修改
    public String updaAddrr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        //修改需传入数据，因为是修改单个数据，所以只需aid即可
        Map<String, String[]> parameterMap = request.getParameterMap();
        Address ad = new Address();
        BeanUtils.populate(ad,parameterMap);
        ad.setUid(user.getUid());
        boolean b= as.updateAddr(ad);
        return Const.SysConst.REDIRECT+"addr?method=getAddr";
    }

}
